﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Ession;
using System.Net.NetworkInformation;
//using System.Net.NetworkInformation;

namespace War3HostConnectionManager
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
            TcpConnections = new List<TcpConnection>();
            lvwColumnSorter = new ListViewColumnSorter();

            this.lvwlistView1.ListViewItemSorter = lvwColumnSorter;
            tmrtimer1.Start();
        }
        private ListViewColumnSorter lvwColumnSorter;
        private List<Ession.TcpConnection> TcpConnections;
        private string[] connections;
        private void btn_Click(object sender, EventArgs e)
        {
            //tmrtimer1.Stop();
          //   tmrtimer1.Start();
            //  tmrtimer1_Tick(null, null);
            connections = Ession.TcpDisconnecter.Connections(Ession.TcpDisconnecter.State.Established);
            TcpConnections.Clear();
            lvwlistView1.Items.Clear();
            foreach (var connection in connections)
            {
                var c = new TcpConnection(connection);
                if (!TcpConnections.Contains(c))
                {
                    if (c.LocalIP.Contains("127.0.0.1") && (c.LocalPort != 6112 && c.LocalPort != 6113 && c.LocalPort != 6114))
                    {
                        TcpConnections.Add(c);
                    }
                    //  TcpConnections.Add(c);

                }


            }
            int i = 1;
            foreach (var tcpConnection in TcpConnections)
            {

                ListViewItem lvi = new ListViewItem(i.ToString());
                lvi.SubItems.Add(tcpConnection.LocalIP);
                lvi.SubItems.Add(tcpConnection.LocalPort.ToString());
                lvi.SubItems.Add(tcpConnection.RemoteIP.ToString());
                lvi.SubItems.Add(tcpConnection.RemotePort.ToString());
                lvi.SubItems.Add(tcpConnection.State.ToString());


                lvwlistView1.Items.Add(lvi);
                i++;
            }
        }

        private void lvwlistView1_ColumnClick(object sender, ColumnClickEventArgs e)
        {

            if (e.Column == lvwColumnSorter.SortColumn)
            {
                // 重新设置此列的排序方法.
                if (lvwColumnSorter.Order == SortOrder.Ascending)
                {
                    lvwColumnSorter.Order = SortOrder.Descending;
                }
                else
                {
                    lvwColumnSorter.Order = SortOrder.Ascending;
                }
            }
            else
            {
                // 设置排序列，默认为正向排序
                lvwColumnSorter.SortColumn = e.Column;
                lvwColumnSorter.Order = SortOrder.Ascending;
            }

            // 用新的排序方法对ListView排序
            this.lvwlistView1.Sort();



        }

        private void 关闭连接ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private string port;
        private void tmrtimer1_Tick(object sender, EventArgs e)
        {
          
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定关闭端口 " + port + "吗?", "", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                Ession.TcpDisconnecter.CloseLocalPort(int.Parse(port));
               
            }
        }

        private void lvwlistView1_MouseClick(object sender, MouseEventArgs e)
        {

            ;
        }

        private void lvwlistView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvwlistView1.SelectedItems.Count > 0)
            {
                var item = lvwlistView1.SelectedItems[0];
                if (item == null)
                {
                    return;
                }
                port = item.SubItems[2].Text;
            }


        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            //  int i = 0;
            //foreach (var tcpConnection in TcpConnections)
            //{
            //    //Ping p = new Ping();
            //    //var r = p.Send(tcpConnection.RemoteIP, 1000);
            //    //if (r.Status == IPStatus.Success)
            //    //{
            //    //    lvwlistView1.Items[i].SubItems.Add(r.RoundtripTime.ToString());
            //    //}
            //    //else//超时
            //    //{
            //    //    lvwlistView1.Items[i].SubItems.Add("timeOut");

            //    //}
            //    //i++;

            //}
            var fs = TcpConnections.OrderByDescending(s => s.LocalPort).ToList();
            //foreach (var tcpConnection in fs)
            //{
            //    MessageBox.Show(tcpConnection.LocalPort.ToString());
            //}
            int i = 0;
            if (fs.Count > 3)//连接数多于三个
            {
                Ession.TcpDisconnecter.CloseLocalPort(fs[0].LocalPort);
                fs.Remove(fs[0]);
                i++;
                Thread.Sleep(1000);

                Ession.TcpDisconnecter.CloseLocalPort(fs[0].LocalPort);
                fs.Remove(fs[0]);
                i++;
                Thread.Sleep(800);
                Ession.TcpDisconnecter.CloseLocalPort(fs[0].LocalPort);

                fs.Remove(fs[0]);
                i++;
                Thread.Sleep(500);

                foreach (var tcpConnection in fs)
                {
                    if (i > 9)
                    {
                        return;

                    }
                    Ession.TcpDisconnecter.CloseLocalPort(tcpConnection.LocalPort);
                    i++;
                    Thread.Sleep(300);
                }

            }
            else
            {
                foreach (var tcpConnection in fs)
                {
                    Ession.TcpDisconnecter.CloseLocalPort(tcpConnection.LocalPort);
                    Thread.Sleep(700);
                }
            }

        }
    }
}
